package io.finer.erp.jeecg.bas.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import io.finer.erp.jeecg.bas.entity.BasPrinter;
import io.finer.erp.jeecg.bas.mapper.BasPrinterMapper;
import io.finer.erp.jeecg.bas.service.IBasPrinterService;
import io.finer.erp.jeecg.jolimark.comm.Config;
import io.finer.erp.jeecg.jolimark.comm.HttpClientUtil;
import io.finer.erp.jeecg.jolimark.model.BindModel;
import io.finer.erp.jeecg.jolimark.model.PrintRequestModel;
import io.finer.erp.jeecg.jolimark.print.CreateAccessToken;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description: 打印机管理
 * @Author: jeecg-boot
 * @Date:   2021-03-25
 * @Version: V1.0
 */
@Service
public class BasPrinterServiceImpl extends ServiceImpl<BasPrinterMapper, BasPrinter> implements IBasPrinterService {
    @Override
    public boolean bindPrinter(BasPrinter basPrinter) throws RuntimeException {
        if(StringUtils.isBlank(basPrinter.getId())){
            throw new RuntimeException("打印机信息获取失败！");
        }
        basPrinter = baseMapper.selectById(basPrinter.getId());
        if(basPrinter == null){
            throw new RuntimeException("打印机信息获取失败！");
        }
        StringBuffer deviceCode = new StringBuffer();
        deviceCode.append(basPrinter.getCode());
        deviceCode.append("#");
        deviceCode.append(basPrinter.getDevice());
        BindModel bindModel = new BindModel();
        bindModel.setAccessToken(Config.getAccessToken());
        bindModel.setAppId(Config.getAppID());
        bindModel.setDeviceCodes(deviceCode.toString());
        try {
            bind(bindModel);
        } catch (RuntimeException e) {
            throw new RuntimeException(e.getMessage());
        }
        basPrinter.setStatus(1);
        return SqlHelper.retBool(getBaseMapper().updateById(basPrinter));
    }

    /**
     * 打印方法
     *
     * @param model       打印请求参数
     */
    protected void bind(BindModel model) throws RuntimeException{

        String params = convert(model);
        String urlString = Config.getUrlV3() + "BindPrinter";

        String responseString = HttpClientUtil.sendHttpPost(urlString, params);
        JSONObject responseJson = JSON.parseObject(responseString);
        if(!responseJson.getInteger("return_code").equals(0) && !responseJson.getInteger("return_code").equals(10103)){
            throw new RuntimeException(responseJson.getString("return_msg"));
        }
        if(responseJson.getInteger("return_code").equals(10103)){
            CreateAccessToken createAccessToken = new CreateAccessToken();
            createAccessToken.accessToken();
            bind(model);
        }
    }

    /**
     * 将打印参数对象转成字符串形式键值对
     *
     * @param model 打印请求参数
     * @return 字符串键值对
     */
    protected String convert(BindModel model) {
        StringBuilder result = new StringBuilder();
        result.append("app_id=").append(model.getAppId());
        result.append("&access_token=").append(model.getAccessToken());
        result.append("&device_codes=").append(model.getDeviceCodes());

        return result.toString();
    }
}
